
#import "WWImageButtonCell.h"

@implementation WWImageButtonCell


// need to reimplement the method for setting the button's image to autogenerate an altImage...


- drawInside:(const NXRect *)aRect inView:controlView
{
   NXPoint  p;

   // really should determine what state I'm in...
   
   if ([icon.bmap.normal respondsTo:@selector(composite:toPoint:)])
   {  p.x = 0.0;
      if ([controlView isFlipped])  
      {  p.y = aRect->size.height - 1.0;
      }
      else
      {  p.y = 0.0;
      }
      if ([self state])
      {  [icon.bmap.normal composite:NX_SOVER toPoint:&p];
      }
      else
      {  [icon.bmap.alternate composite:NX_SOVER toPoint:&p];
      }
   }
   else
   {  [super drawInside:aRect inView:controlView];
   }

   return self;
}

#if 0
- (BOOL)continueTracking:(const NXPoint *)lastPoint at:(const NXPoint *)currentPoint inView:controlView
{
  NXRect         boundsRect;
  id             theBitmap;
  NXCoord        transformedY;
  float          red, green, blue, alpha;


  // if currentPoint is in ControlView, return YES, otherwise NO
  [controlView getBounds:&boundsRect];

  // check X
  if (boundsRect.origin.x > currentPoint->x)
  {  NXLogError("NO (boundsRect.origin.x > currentPoint->x) : %f > %f\n", boundsRect.origin.x, currentPoint->x);
     return NO;
  }
  if ((boundsRect.origin.x + boundsRect.size.width) < currentPoint->x)
  {  NXLogError("NO (boundsRect.origin.x + boundsRect.size.width) < currentPoint->x) : %f > %f\n", 
		boundsRect.origin.x + boundsRect.size.width, currentPoint->x);
     return NO;
  }

  // check Y 
  if (boundsRect.origin.y > currentPoint->y)
  {  NXLogError("NO (boundsRect.origin.y > currentPoint->y) : %f > %f\n", boundsRect.origin.y, currentPoint->y);
     return NO;
  }
  if ((boundsRect.origin.y + boundsRect.size.height) < currentPoint->y)
  {  NXLogError("NO (boundsRect.origin.y + boundsRect.size.height) < currentPoint->y) : %f > %f\n", 
		boundsRect.origin.y + boundsRect.size.height, currentPoint->y);
     return NO;
  }

  // if the button doesn't have an image, we're done
  if (![self image])
  {  NXLogError("YES ![self image]\n");
     return YES;
  }

  // otherwise, we now need to determine if it lies within the image
  theBitmap = [[self image] bestRepresentation];

  // if the x component is greater than the width of the image, we're outta here
  if (currentPoint->x > [theBitmap pixelsWide])
  {  NXLogError("NO currentPoint->x > [theBitmap pixelsWide] : %f > %f\n", currentPoint->x, [theBitmap pixelsWide]);
     return NO;
  }

  // if the y component is greater than the height of the image, we're outta here
  transformedY = currentPoint->y;
  if ([controlView isFlipped])
  {  transformedY = (boundsRect.size.height - currentPoint->y);
  }
  if (transformedY > [theBitmap pixelsHigh])
  {  NXLogError("NO (transformedY) > [theBitmap pixelsHigh] : %f > %f\n", 
		   (transformedY), [theBitmap pixelsHigh]);
        return NO;
  }

  // so now we know that we have an image, it has some opacity, and
  // the current point lies within the bounds of the image.  We now need to
  // determine if the point corresponds to some (at least partially) opaque
  // part of the image.
  NXConvertColorToRGBA(NXReadPixel(currentPoint), &red, &green, &blue, &alpha);
  if (alpha > 0.0)
  {  NXLogError("YES pixel at (%d, %d) is rgba (%f, %f, %f, %f)\n", 
		currentPoint->x, currentPoint->y, red, green, blue, alpha);
     return YES;
  }

  NXLogError("NO pixel at (%d, %d) is rgba (%f, %f, %f, %f)\n", 
	     currentPoint->x, currentPoint->y, red, green, blue, alpha);
  return NO;
}

#define ACTIVEBUTTONMASK (NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK|NX_MOUSEDOWNMASK)
- (BOOL)trackMouse:(NXEvent *)theEvent inRect:(const NXRect *)cellFrame ofView:controlView
{
  NXPoint    oldMouse, newMouse;
  int        oldMask;
  BOOL       mouseUp;


  // start playing sound...
  [sound play];
  NXLogError("started tracking mouse...\n");
  oldMouse = theEvent->location;
  [controlView convertPoint:&oldMouse fromView:nil];

  [controlView lockFocus];
  oldMask = [[controlView window] addToEventMask:ACTIVEBUTTONMASK];
  while (1)
  { newMouse = theEvent->location;
    [controlView convertPoint:&newMouse fromView:nil];
    theEvent = [NXApp getNextEvent:ACTIVEBUTTONMASK];
    if ((theEvent->type == NX_MOUSEUP) || ![self continueTracking:&oldMouse at:&newMouse inView:controlView])
    {  break;
    }
    else
    {  oldMouse = newMouse;
    }
  }
  [controlView unlockFocus];
  [[controlView window] setEventMask:oldMask];
  mouseUp = (theEvent->type == NX_MOUSEUP);
  if (mouseUp)
  {  NXLogError("mouseUp!\n");
  }
  // at this point, we've either let the mouse button up or left the bounds of the button
  if ((mouseUp) && [self continueTracking:&oldMouse at:&newMouse inView:controlView])
  {  [self incrementState];
     NXLogError("sending Action - mouseUp == %d\n", mouseUp);
     [controlView sendAction:[controlView action] to:[controlView target]];
     NXLogError("YES: stopped tracking mouse...\n");
     return YES;
  }

  // stop sound...  

  NXLogError("NO: stopped tracking mouse...\n");
  return NO;
}
#endif

- setIcon:(const char *)iconName
{
  [super setIcon:iconName];
  [[[self image] bestRepresentation] setOpaque:NO];
  return self;
}

- setImage:theImage
{
  [super setImage:theImage];
  [[[self image] bestRepresentation] setOpaque:NO];
  return self;
}

- setAltIcon:(const char *)iconName
{
  [super setAltIcon:iconName];
  [[[self altImage] bestRepresentation] setOpaque:NO];
  return self;
}

- setAltImage:theImage
{
  [super setImage:theImage];
  [[[self altImage] bestRepresentation] setOpaque:NO];
  return self;
}

@end
